<?php
class masterkey_ctl_admin_errorlist extends desktop_controller
{

    /*
    *  异常发货单列表
    */
    public function index()
    {
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $pagelimit = 20;
        $offset = ($page-1) * $pagelimit;
        //查询有ome发货单却没有wms发货单数据
        $rs = $this->getSql($offset,$pagelimit);
        //仓库数据
        $brObj = app::get('ome')->model('branch');
        $row = $brObj->getList('branch_id, name',array('b_type'=>1),0,-1);
        $branch = array();
        foreach($row as $key=>$val){
            $branch[$val['branch_id']] = $val;
        }
        //发货状态
        $status = array (
            'succ' => '已发货',
            'failed' => '发货失败',
            'cancel' => '已取消',
            'progress' => '等待配货',
            'timeout' => '超时',
            'ready' => '等待配货',
            'stop' => '暂停',
            'back' => '打回',
            'return_back'=>'退回',
        );
        foreach($rs as &$data){
            $data['status']      = $status[$data['status']];
            $data['logi_no']     = $data['logi_no'] ? $data['logi_no'] : "无";
            $data['is_received'] = $data['is_received']==0 ? "否" : "是";
            $data['branch_name'] = $branch[$data['branch_id']]['name'];
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
        }
        //总数量
        $total = count($this->getSql(0,100000));
        $this->pagedata['data'] = $rs;
        $this->pagedata['pagelimit'] = $pagelimit;
        $this->pagedata['page'] = $page;
        $this->pagedata['total']= $total;
        $this->page('admin/error_list.html');
    }
    //查询sql
    public function getSql($offset,$pagelimit)
    {
        $db = kernel::database();
        //查询有ome发货单却没有wms发货单数据
        $sql_find = "select ode.delivery_id,ode.delivery_bn,ode.logi_name,ode.logi_no,ode.ship_province,ode.ship_city,ode.ship_district,ode.ship_addr,ode.branch_id,ode.ship_name,ode.create_time,ode.status,ode.is_received from sdb_ome_delivery ode where not exists (select outer_delivery_bn from sdb_wms_delivery where outer_delivery_bn=ode.delivery_bn) ORDER BY ode.create_time DESC LIMIT $offset,$pagelimit";
        $rs = $db->select($sql_find);
        return $rs;
    }


    //更新生成异常发货单列表oms
    public function updateWms()
    {
        $page = $_GET['page'];
        $pagelimit = 20;
        $offset = ($page-1) * $pagelimit;
        //更改有ome发货单却没有wms发货单数据
        $rs = $this->getSql($offset,$pagelimit);
        foreach($rs as $val){
            //发货单创建
            // $this->begin('');
            // kernel::database()->exec('commit');
            $oOperation_log = app::get('ome')->model('operation_log');
            $sendObj = app::get('console')->model('delivery_send');
             $delivery = app::get('ome')->model('delivery')->dump(array('delivery_id'=>$val['delivery_id'],'stock_status'=>'false','deliv_status'=>'false','expre_status'=>'false','pause'=>'false','process'=>'false','status'=>array('progress','ready')),'delivery_id');
            if ($delivery) {
                ome_delivery_notice::create($val['delivery_id']);
            }
            // $this->end(true, '命令已经被成功发送！！');
        }
        $msg="操作成功";
        $this->splash('success',"index.php?app=masterkey&ctl=admin_errorlist&act=index",$msg,'redirect',array());
    }
}
